package com.shirley.customcontrols;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.fl_content)
    FrameLayout flContent;
    @BindView(R.id.tv_tab1)
    TextView tvTab1;
    @BindView(R.id.tv_tab2)
    TextView tvTab2;
    @BindView(R.id.tv_tab3)
    TextView tvTab3;
    @BindView(R.id.tv_tab4)
    TextView tvTab4;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    /**
     * 三个标签页的TextView控件
     */
    private TextView[] tabTexts;
    /**
     * 三个标签页对应的Fragment的canonicalName(包含包名的完整类名)
     */
    private Fragment[] mFragments = {new Fragment1(), new Fragment2(), new Fragment3(),
            new Fragment4()};

    // 主页三个标签的索引值
    private static final int TAB1 = 0;
    private static final int TAB2 = 1;
    private static final int TAB3 = 2;
    private static final int TAB4 = 3;

    private Fragment mCurFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setSupportActionBar(toolbar);
        ButterKnife.bind(this);

        // 初始化三个标签页按钮和三个标签页面
        tabTexts = new TextView[]{tvTab1, tvTab2, tvTab3, tvTab4};

        // 将fragment的名字和tab对应起来，之后可以根据tab切换正确的fragment
        for (int i = 0; i < tabTexts.length; i++) {
            tabTexts[i].setTag(mFragments[i]);
        }

        // 默认选中第一个标签页
        selectTab(TAB1);
    }

    /**
     * 切换标签页的状态
     *
     * @param index 被选中的标签页按钮的索引值，从左到右为0/1/2
     */
    private void selectTab(int index) {
        // 刷新设置所有tab为非选中状态
        for (int i = 0; i < tabTexts.length; i++) {
            tabTexts[i].setSelected(false);
        }
        // 设置被选中的标签为选中状态
        tabTexts[index].setSelected(true);
        // 切换fragment
        switchFragment((Fragment) tabTexts[index].getTag());
    }

    public void switchFragment(Fragment to) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        if (mCurFragment == null){
            transaction.add(R.id.fl_content, to).commit(); // 首次add第一个Fragment到Activity中
        } else {
            if (mCurFragment != to) {
                if (!to.isAdded()) {	// 先判断是否被add过
                    transaction.hide(mCurFragment).add(R.id.fl_content, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
                } else {
                    transaction.hide(mCurFragment).show(to).commit(); // 隐藏当前的fragment，显示下一个
                }
            }
        }
        mCurFragment = to;
    }

    @OnClick({R.id.tv_tab1, R.id.tv_tab2, R.id.tv_tab3, R.id.tv_tab4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_tab1:
                selectTab(TAB1);
                break;
            case R.id.tv_tab2:
                selectTab(TAB2);
                break;
            case R.id.tv_tab3:
                selectTab(TAB3);
                break;
            case R.id.tv_tab4:
                selectTab(TAB4);
                break;
        }
    }
}
